using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Microsoft.Extensions.DependencyInjection;
using SukiUI.Controls;
using welog.Utilities;
using SukiUI.Models;
using welog.Features;
using welog.Features.ControlsLibrary;
using TerraFX.Interop.Windows;

namespace welog;

public partial class MainWindowView : SukiWindow
{
    
    public MainWindowView()
    {
        InitializeComponent();
        var bitmap = new Bitmap(AssetLoader.Open(new Uri("avares://welog/Assets/OIG.N5o-removebg-preview.png")));
        Icon = new WindowIcon(bitmap);
        
        var  windowHandle =(HWND)GetTopLevel(this).TryGetPlatformHandle().Handle;
        //AvaloniaWinProcHelper.RegisterHotKey(WindowHandle, 1, MOD.MOD_CONTROL, VK.VK_F24, () =>
        AvaloniaWinProcHelper.RegisterHotKey(windowHandle, 1, MOD.MOD_CONTROL, 81, () =>
        {
            if (this.IsVisible)
            {
                this.Hide();
            }
            else
            {
                this.WindowState = WindowState.FullScreen;
                this.Show();
            }
        });
    }

    private void MenuItem_OnClick(object? sender, RoutedEventArgs e)
    {
        if (DataContext is not MainWindowViewModel vm) return;
        if (e.Source is not MenuItem mItem) return;
        if (mItem.DataContext is not SukiColorTheme cTheme) return;
        vm.ChangeTheme(cTheme);
    }

    private void InputElement_OnPointerPressed(object? sender, PointerPressedEventArgs e)
    {
        IsMenuVisible = !IsMenuVisible;
    }
    
    private void Control_OnLoaded(object? sender, RoutedEventArgs e)
    { 
        this.myMenu.IsMenuExpanded=false;
        this.WindowState = WindowState.FullScreen;
       /* var keyboardHook= KeyboardHook.GetKeyboardHook();
        keyboardHook.AddKeyboardHandler((wParam, keyboardHookStruct) =>
       {
           if (KeyboardHook.WM_SYSKEYDOWN == wParam)
           {
               if (this.IsVisible)
               {
                   this.Hide();
               }
               else
               {
                   this.WindowState = WindowState.FullScreen;
                   this.Show();
               }
               
           }
          
       });
        
       keyboardHook.Start();
       */
       
    }

    private void TopLevel_OnClosed(object? sender, EventArgs e)
    {
        var keyboardHook= KeyboardHook.GetKeyboardHook();
        keyboardHook.Stop();
    }

    private void MenuItem_OnClick_HotKey(object? sender, RoutedEventArgs e)
    {
        var myapp = App.Current as App; var welogPageBase=myapp.ServiceProvider.GetServices<WelogPageBase>();
        var mds= welogPageBase.Where(x=>x.GetType().Name=="MarkDownViewModel");
        if (mds.Count()>0)
        {
            var md = mds.FirstOrDefault() as MarkDownViewModel;
            md.IsShow = !md.IsShow;
        }  
    }

    private void WindowBase_OnActivated(object? sender, EventArgs e)
    {
        this.WindowState = WindowState.FullScreen;
    }
}

public static unsafe class AvaloniaWinProcHelper
{
    private static delegate* unmanaged<HWND, uint, WPARAM, LPARAM, LRESULT> _wrappedAvaloniaWinProc;

    public static HWND WindowHandle { get; set; }

    public static Action hotKeyActionHandle;

    public static void RegisterHotKey(HWND hWnd, int id, uint fsModifiers, uint vk,Action hotKeyAction)
    {
        hotKeyActionHandle=hotKeyAction;
        WindowHandle = hWnd;
        _wrappedAvaloniaWinProc =
            (delegate* unmanaged<HWND, uint, WPARAM, LPARAM, LRESULT>)Windows.GetWindowLongPtr(WindowHandle,
                GWLP.GWLP_WNDPROC);
        delegate* unmanaged<HWND, uint, WPARAM, LPARAM, LRESULT> wrapperWinProc = &WndProcHook;
        Windows.SetWindowLongPtr(WindowHandle, GWLP.GWLP_WNDPROC, (nint)wrapperWinProc);
        Windows.RegisterHotKey(hWnd, id, fsModifiers, vk);
    }

    [UnmanagedCallersOnly]
    public static LRESULT WndProcHook(HWND hwnd, uint code, WPARAM wParam, LPARAM lParam)
    {
        Debug.WriteLine($"msg - {code}");

        if (code == WM.WM_HOTKEY)
        {
            hotKeyActionHandle();
        }

        return _wrappedAvaloniaWinProc(hwnd, code, wParam, lParam);
    }
}